package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Process;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pro.mapper.ProcessMapper;
import com.qidianit.mes.pro.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @ Description:
 * @ Author:WuJingLei
 * @ Date: 2025/9/17 -15:30
 */
@Service
public class ProcessServiceImpl implements ProcessService {
    @Autowired
    private ProcessMapper processMapper;
    @Override
    public Result<PageInfo<Process>> queryProcesses(Process process, int page, int size) {
        //使用分页
        PageHelper.startPage(page,size);
        //查询数据库
        List<Process> processes = processMapper.queryProcesses(process);
        return Result.success(new PageInfo<>(processes));
    }

    @Override
    public Result<Process> addProcess(Process process) {
        Process process1 = processMapper.queryProcess(process.getProcessCode());
        if (process1 !=null){
            return Result.error(500,"编码已存在",new Process());
        }
        return processMapper.saveProcess(process)>0?Result.success(process):Result.error("操作失败");
    }

    @Override
    public Result<Process> queryProcess(String code) {
        return Result.success(processMapper.queryProcess(code));
    }

    @Override
    public Result updateProcess(Process process) {
        Process process1 = processMapper.queryProcess(process.getProcessCode());
        if (process1 !=null && !Objects.equals(process1.getId(), process.getId())){
            return Result.error(500,"编码已存在",null);
        }
        return processMapper.updateProcess(process)>0?Result.success():Result.error("操作失败");
    }

    @Override
    public Result deleteProcess(Long id) {
        return processMapper.deleteProcess(id)>0?Result.success():Result.error("操作失败");
    }

    @Override
    public Result batchDelete(Long[] ids) {
        return processMapper.batchDelete(ids)>0?Result.success():Result.error("操作失败");
    }
}
